#!/bin/bash

# 安装依赖
sudo apt-get -y install vim redis-server subversion libjpeg8-dev imagemagick libv4l-dev cmake

# 部署Golang运行环境
if [ ! -d "/usr/local/go/bin" ]; then
    sudo wget https://studygolang.com/dl/golang/go1.16.linux-armv6l.tar.gz && sudo tar -C /usr/local -xzf go1.16.linux-armv6l.tar.gz && sudo rm -rf go1.16.linux-armv6l.tar.gz
    sudo sh -c 'echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile'
    source /etc/profile && source .bashrc
    go version
fi

# 安装摄像头所需的软件包并配置开机启动服务
if [ ! -d "/usr/local/lib/mjpg-streamer" ]; then
  sudo wget https://cdn.robomentor.com/install/mjpg-streamer-master.zip && sudo unzip mjpg-streamer-master.zip
  cd mjpg-streamer-master/mjpg-streamer-experimental && sudo make && sudo make install
  cd ~ && sudo rm -rf mjpg-streamer-master.zip && sudo rm -rf mjpg-streamer-master
  sudo touch /etc/systemd/system/camera.service
  sudo sh -c 'echo "[Unit]" >> /etc/systemd/system/camera.service'
  sudo sh -c 'echo "Description=video capture device" >> /etc/systemd/system/camera.service'
  sudo sh -c 'echo "After=network.target" >> /etc/systemd/system/camera.service'
  sudo sh -c 'echo "" >> /etc/systemd/system/camera.service'
  sudo sh -c 'echo "[Service]" >> /etc/systemd/system/camera.service'
  str_a='"/usr/local/lib/mjpg-streamer/input_uvc.so -d /dev/video0 -n -f 30 -r 1280x720"'
  str_b='"/usr/local/lib/mjpg-streamer/output_http.so -p 8282"'
  str_c="ExecStart=/usr/local/bin/mjpg_streamer -i $str_a -o $str_b"
  export str_c
  sudo -E sh -c 'echo $str_c >> /etc/systemd/system/camera.service'
  sudo sh -c 'echo "" >> /etc/systemd/system/camera.service'
  sudo sh -c 'echo "[Install]" >> /etc/systemd/system/camera.service'
  sudo sh -c 'echo "WantedBy=multi-user.target" >> /etc/systemd/system/camera.service'
  sudo systemctl enable camera.service
fi

sudo redis-cli -h 127.0.0.1 -p 6379 -n 5 set platform "raspberrypi"

# 始终部署最新版本
sudo curl -s https://cdn.robomentor.com/install/update.sh|bash
